package org.hentai.component;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class IndexAspect {

    @Before("execution(public String org.hentai.service.IndexService.beforeAspect(String) )")
    public void beforeAspect(JoinPoint joinPoint) {
        System.out.println("beforeAspect " + joinPoint.getSignature().getName());
    }

    public void afterAspect(JoinPoint joinPoint) {}

    @AfterReturning("execution(public String org.hentai.service.IndexService.afterAspect(String))")
    public void afterReturningAspect(JoinPoint joinPoint) {
        System.out.println("afterReturningAspect " + joinPoint.getSignature().getName());
    }

    @Around(value = "execution(public String org.hentai.service.IndexService.aroundAspect(String))")
    public Object around(ProceedingJoinPoint joinPoint) {
        System.out.println("around " + joinPoint.getSignature().getName());

        Object[] args = joinPoint.getArgs();

        Object[] param = new Object[args.length];

        for (int i = 0; i < args.length; i++) {
            param[i] = "hentai" + args[i];
        }

        try {
            Object result = joinPoint.proceed(param);

            if (result instanceof String) {
                return "hentai lolicon " + result;
            }

            return result;

        } catch (Throwable e) {
            return null;
        }

    }

}
